package com.shire.shire_interface.controller.marketing;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.marketing.WxNewResponse;
import com.shire.shire_interface.model.marketing.WxNewResponseExample;
import com.shire.shire_interface.model.marketing.WxNewResponseRecord;
import com.shire.shire_interface.model.marketing.WxNewResponseRecordExample;
import com.shire.shire_interface.service.marketing.WxNewResponseRecordSservice;
import com.shire.shire_interface.service.marketing.WxNewResponseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//@Api(tags = "营销  新好友应答")
@RestController
@RequestMapping("/newResponse")
public class WxNewResponseController extends BaseController {

    @Resource
    private WxNewResponseService wxNewResponseService;
    @Resource
    private WxNewResponseRecordSservice wxNewResponseRecordSservice;

//    @ApiOperation(value = "添加设置")
    @ResponseBody
    @PostMapping("/insert")
    public Response insert(@RequestBody WxNewResponse wxNewResponse, HttpSession session){
        Response response = new Response();
        try {
            if (!checkLoginUserByCsId(session, wxNewResponse.getCsid())){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            wxNewResponse.setCreatedate(new Date());
            wxNewResponseService.insertSelective(wxNewResponse);

            response.setData(wxNewResponse);
            response.success("操作成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }


//    @ApiOperation(value = "修改设置")
    @ResponseBody
    @PostMapping("/update")
    public Response update(@RequestBody WxNewResponse wxNewResponse, HttpSession session){
        Response response = new Response();
        try {
            if (!checkLoginUserByCsId(session, wxNewResponse.getCsid())){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            wxNewResponse.setUpdatedate(new Date());
            wxNewResponseService.updateSelective(wxNewResponse);

            response.setData(wxNewResponse);
            response.success("操作成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }


//    @ApiOperation(value = "查询 csId, wxAccount")
    @ResponseBody
    @PostMapping("/get")
    public Response get(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            String wxAccount = jsonObject.getString("wxAccount");

            WxNewResponseExample wxNewResponseExample = new WxNewResponseExample();
            wxNewResponseExample.createCriteria().andCsidEqualTo(csId).andWxaccountEqualTo(wxAccount);
            wxNewResponseExample.setOrderByClause("id desc");
            List<WxNewResponse> wxNewResponses = wxNewResponseService.selectByExample(wxNewResponseExample);

            WxNewResponseRecordExample wxNewResponseRecordExample = new WxNewResponseRecordExample();

            for (WxNewResponse wxNewRespons : wxNewResponses) {
                Integer id = wxNewRespons.getId();
                wxNewResponseRecordExample.createCriteria().andCsidEqualTo(csId).andWxaccountEqualTo(wxAccount).andRespidEqualTo(id);
                List<WxNewResponseRecord> wxNewResponseRecords = wxNewResponseRecordSservice.selectByExample(wxNewResponseRecordExample);
                wxNewRespons.setResponseRecordList(wxNewResponseRecords);
            }
            response.setData(wxNewResponses);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "删除 id, csId, wxAccount")
    @ResponseBody
    @PostMapping("/del")
    public Response del(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();

        try {
            Integer id = jsonObject.getInteger("id");
            Integer csId = jsonObject.getInteger("csId");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            String wxAccount = jsonObject.getString("wxAccount");
            wxNewResponseService.deleteByPrimaryKey(id);

            WxNewResponseRecordExample wxNewResponseRecordExample = new WxNewResponseRecordExample();
            wxNewResponseRecordExample.createCriteria().andCsidEqualTo(csId).andRespidEqualTo(id).andWxaccountEqualTo(wxAccount);
            wxNewResponseRecordSservice.deleteByExample(wxNewResponseRecordExample);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }


        return response;
    }
}
